package com.jyun;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class ServletDemo extends HttpServlet {
    @Override
    public void destroy() {
        System.out.println("destroy");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName=req.getParameter("username");
        String password=req.getParameter("password");
        PrintWriter out=new PrintWriter(resp.getOutputStream());
        out.println("This is Post request!");

        ConnectDataBase userDataBase=
                new ConnectDataBase("jdbc:mysql://127.0.0.1:3306/servletuser?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true",
                        "fengdada","fenglingdao");
        String pwd=userDataBase.select("userandpwd",userName);
        if(pwd.equals(password)){
            out.println("welcome! dear "+userName);
            Date date=new Date();
            out.println("Time:"+date);
        }
        else{
            out.println("username or password is error!");
            resp.sendRedirect("/login.jsp");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out=new PrintWriter(resp.getOutputStream());
        out.print("This is Get request!");
        out.flush();
        out.close();
    }

}